#include "XRound_border.h"
#include "QGraphicsDropShadowEffect"
#include <QDebug>

Round_Button::Round_Button(QWidget *parent):QPushButton(parent)
{


    m_text = "";
    textColor.setRgb(0,0,0);
    m_releaseColor=QColor(255,255,255, 255);
    m_pressColor=QColor(220,220,220, 255);
    m_color =m_releaseColor;

    m_x=0;
    m_y = 0;
    m_h = height();
    m_w = width();
}

void Round_Button::setTxet(const QString& text)
{
    m_text=text;
}

void Round_Button::setTxetFontAndColor(QFont font, QColor color)
{
    m_font = font;
    textColor = color;
}


void Round_Button::paintEvent(QPaintEvent *event)
{
    if(m_isfirst)
    {
        m_isfirst = false;
        m_h = height();
        m_w = width();
    }
    QPainter painter(this);


    painter.setRenderHint(QPainter::Antialiasing);
    painter.setViewport(0, 0, width(), height());
    painter.setWindow(0, 0, width(), height());
    QPainterPath path;
    painter.setPen(Qt::NoPen);//setPen会带边框

    //绘制圆型框
    path.addRoundedRect(m_x, m_y, m_w, m_h, 20, 20);
    painter.setClipPath(path);
    QBrush Brush(m_color);
    painter.setBrush(Brush);
    painter.drawRect(m_x, m_y, m_w, m_h);


  //绘制文本
    QBrush textbrush(textColor);

    if(m_ishover)
    {

        textbrush.setColor(textColor);
    }


    painter.setBrush(textbrush); // 文本填充色
    QPainterPath textPath;
    textPath.addText(20,23, m_font, m_text);
    QRectF textBounds = textPath.boundingRect();
    painter.translate((width()/2)-textBounds.width()-5,(height()/2) - textBounds.height()
    );
    //画出字体
     painter.drawPath(textPath);
}

void Round_Button::mousePressEvent(QMouseEvent *event)
{
    if(event->button() != Qt::LeftButton)
    {
        return ;
    }
    m_color = m_pressColor;
    m_x =3;
    m_y =3;

    m_h = height()*0.95;
    m_w = width()*0.95;
    update();

}

void Round_Button::mouseReleaseEvent(QMouseEvent *ev)
{
    if(ev->button() != Qt::LeftButton)
    {
        return ;
    }
    m_x=0;
    m_y=0;
    m_color= m_releaseColor;
    m_h = height();
    m_w = width();
    update();
   emit m_clicked();
}

//鼠标移入是字体变亮
void Round_Button::enterEvent(QEvent *event)
{
    m_ishover =true;
     textColor.setAlpha(160);
     update();
}
//鼠标移出时字体恢复
void Round_Button::leaveEvent(QEvent *event)
{
     m_ishover =false;
     textColor.setAlpha(255);
     update();
}

void Round_Button::setPressColor(const QColor color)
{
    m_pressColor = color;
}

void Round_Button::setColor(const QColor color)
{
     m_color = color;
     m_releaseColor = color;
}



